#!/usr/bin/env python

#
#    Copyright (c) 2009 Corey Goldberg (corey@goldb.org)
#    License: GNU GPLv3
#
#    This file is part of PerfMetrics.
#    
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.  See the GNU General Public License 
#    for more details.
#


import ConfigParser
import os


def main():
    config = ConfigParser.ConfigParser()
    config.read('config.ini')
    
    # create output directories if they don't exist
    if not os.path.exists('report'):
        os.makedirs('report')
    if not os.path.exists('report\\graphs'):
        os.makedirs('report\\graphs')
        
    # create main index
    file_name = 'report/index.html'
    print 'generating %s' % file_name
    f = open(file_name, 'w')
    f.write('<html><head><title>performance metrics</title></head><body>\n')
    for section in sorted(config.sections()):
        if section != 'global':
            host = config.get(section, 'host')
            link_name = '%s.html' % host
            f.write('<a href="%s">%s</a><br />\n' % (link_name, host))
    f.write('</body>\n')
    f.close()
    
    # create detail pages
    graph_mins = config.get('global', 'graph_mins').split(',')
    for section in config.sections():
        if section != 'global':
            host = config.get(section, 'host')
            file_name = 'report/%s.html' % host
            collectors = config.get(section, 'collectors').split(',')
            print 'generating %s' % file_name
            f = open(file_name, 'w')
            f.write('<html><head><title>%s - performance metrics</title></head><body>\n' % host)
            for graph_min in graph_mins:
                f.write('<hr />\n')
                f.write('<h4>past %s mins (%.2f hours)</h4>\n' % (graph_min, (float(graph_min) / 60.0)))
                for collector in collectors:        
                    f.write('<img src="../graphs/%s_%s.rrd_%s.png"></img>\n' % (host, collector, graph_min))
            f.write('</body></html>\n')
            f.close()
                    

if __name__ == '__main__':
    main()

